package com.jsgt.greekfire.repository

import com.jsgt.greekfire.bean.*
import com.jsgt.greekfire.common.http.ResponseData
import com.jsgt.greekfire.common.http.RetrofitManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class TradingRepository : BaseRepository() {
    companion object {
        val instance = TradingRepository()
    }

    /**
     * 交易对列表
     */
    suspend fun getEntrustCoinList(): ResponseData<List<EntrustCoin>> =
        withContext(Dispatchers.IO) {
            RetrofitManager.tradingService.getEntrustCoinList()
        }

    /**
     * 获取k线图数据
     */
    suspend fun getTickMarket(delKey: String): ResponseData<TickMarket> =
        withContext(Dispatchers.IO) {
            RetrofitManager.tradingService.getTickMarket(delKey)
        }

    /**
     * 买入卖出交易
     * "dealType"  1-买入  2-卖出
     * "dealNum"  交易数量
     * "dealAmount"  交易价格
     * "coinCode"  "GFT"
     * "fixedCode"  "USDT"
     * "dealWay" 1
     */
    suspend fun addUserEntrust(params: Map<String, Any>): ResponseData<Any> =
        withContext(Dispatchers.IO) {
            RetrofitManager.tradingService.addUserEntrust(parseBody(params))
        }

    /**
     * 获取委托
     * "dealStatus" 6-当前委托  7-历史委托
     * "page" 当前页码
     * "limit" 每页显示记录数
     */
    suspend fun getUserEntrust(params: Map<String, Any>): ResponseData<UserEntrust> =
        withContext(Dispatchers.IO) {
            RetrofitManager.tradingService.getUserEntrust(parseBody(params))
        }

    /**
     * 取消委托
     * "coinCode": "GFT",
     * "fixedCode": "USDT",
     * "guid": 85
     */
    suspend fun cancelUserEntrust(params: Map<String, Any>): ResponseData<Any> =
        withContext(Dispatchers.IO) {
            RetrofitManager.tradingService.cancelUserEntrust(parseBody(params))
        }

    /**
     * 获取最新价格
     */
    suspend fun getNewPrice(delKey: String): ResponseData<NewPrice> =
        withContext(Dispatchers.IO) {
            RetrofitManager.tradingService.getNewPrice(delKey)
        }

    /**
     * 获取GFT k线数据
     * "dealPair": "GFT_USDT",
     * "ktype": "1min",
     * "startNo": 0,
     * "endNo": 100
     */
    suspend fun getGFTKLineData(params: Map<String, Any>): ResponseData<List<GFTKLineData>> =
        withContext(Dispatchers.IO) {
            RetrofitManager.tradingService.getGFTKLineData(parseBody(params))
        }

    /**
     * 获取GFT简介
     */
    suspend fun getCoinIntroduce(delKey: String): ResponseData<CoinIntroduce> =
        withContext(Dispatchers.IO) {
            RetrofitManager.tradingService.getCoinIntroduce(delKey)
        }

    /**
     * 获取GFT成交数据
     * "dealPair": "GFT_USDT",
     * "size": 100
     */
    suspend fun getTickList(params: Map<String, Any>): ResponseData<List<Tick>> =
        withContext(Dispatchers.IO) {
            RetrofitManager.tradingService.getTickList(parseBody(params))
        }
}